package com.delio.client.pagos.contratos;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.idioma.DelioMessages;
import com.delio.client.pagos.aportes.Aportes;
import com.delio.client.util.Utilities;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;

public class PanelGridContrato extends ContentPanel{
	
	private DelioConstants k = GWT.create(DelioConstants.class);
	private DelioMessages msg = GWT.create(DelioMessages.class);

	private Button btnDelete;
	private Button btnCancelar;
	private ToolBar toolbar;
	private PagingToolBar toolBarPag;

	private List<ColumnConfig> columns;
	private CheckBoxSelectionModel<BeanModel> sm;
	private ColumnModel cm;
	public static Grid<BeanModel> grid;

	private BeanModel selectedModel;
	private List<BeanModel> selectedModels;
	private Integer selectedSize;

	private Listener<MessageBoxEvent> listener;
	private SelectionListener<ButtonEvent> sl_delete;
	
	private GridFilters filters;
	public static PanelNuevoContrato panelnuevocontrato;
	


	public PanelGridContrato() {
		super();
		 setBodyBorder(false);
		 setBorders(false);
		setAnimCollapse(false);
		setIcon(IconHelper.createPath("images/icons/table.png"));
		setHeaderVisible(false);
		setLayout(new FitLayout());

		Contratos.getPaginate();
		btnDelete = new Button(k.eliminar(),IconHelper.createPath("images/icons/delete.gif"));
		btnDelete.setEnabled(false);
		
		btnCancelar=new Button("Cancelar contrato",IconHelper.createPath("images/icons/cancel.png"));
		btnCancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				Contratos.terminarcontrato();
			}
		});
		btnCancelar.setEnabled(false);

		toolbar = new ToolBar();
		toolbar.setStyleAttribute("backgroundImage","url(gxt/images/default/panel/white-top-bottom.gif)");
		toolbar.setBorders(true);
		toolbar.add(btnDelete);
		toolbar.add(btnCancelar);
		toolbar.add(Utilities.getLabel("Detalle de contrato","350px"));
		
		
		
		filters = new GridFilters();
		filters.addFilter(new StringFilter("numero"));
		filters.addFilter(new StringFilter("cargo"));
		filters.addFilter(new StringFilter("sueldo"));
		filters.addFilter(new StringFilter("formpago"));
		filters.addFilter(new StringFilter("turno"));
		filters.addFilter(new StringFilter("tipoempleo"));
		filters.addFilter(new StringFilter("fechainicio"));
		filters.addFilter(new StringFilter("duracion"));
		filters.addFilter(new StringFilter("tipocontrato"));
		filters.addFilter(new StringFilter("estado"));
		filters.addFilter(new StringFilter("fechatermino"));
		
		toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		toolBarPag.bind(Contratos.loader);

		columns = new ArrayList<ColumnConfig>();

		sm = new CheckBoxSelectionModel<BeanModel>();
		sm.setSelectionMode(SelectionMode.SINGLE);

		columns.add(sm.getColumn());
		columns.add(new ColumnConfig("numero", k.numero(), 80));
		columns.add(new ColumnConfig("cargo", k.cargo(), 100));
		columns.add(new ColumnConfig("sueldo", k.sueldo(), 100));
		columns.add(new ColumnConfig("formapago", k.formapago(), 100));
		columns.add(new ColumnConfig("tipocontrato",k.pago(), 80));
		columns.add(new ColumnConfig("turno", k.turno(), 80));
		columns.add(new ColumnConfig("tipoempleo",k.contrato(), 100));
		columns.add(new ColumnConfig("fechainicio", k.fechainicio(), 100));
		columns.add(new ColumnConfig("duracion", k.duracion(), 100));
		columns.add(new ColumnConfig("fechatermino", k.fechatermino(), 70));
		
		GridCellRenderer<BeanModel> change = new GridCellRenderer<BeanModel>() {
			@Override
			public Object render(BeanModel model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore<BeanModel> store, Grid<BeanModel> grid) {

				String val = (String) model.get(property);
		
				String style = "";
				if (val.equalsIgnoreCase("activo"))
					style = "blue";
				else
					style = "red";

				return "<span qtitle='"
						+ cm.getColumnById(property).getHeader() + "' qtip='"
						+ val + "' style='font-weight: bold;color:" + style
						+ "'>" + val + "</span>";
			}
		};

		ColumnConfig column = new ColumnConfig("estado", k.estadocontrato(), 80);
		column.setAlignment(HorizontalAlignment.CENTER);
		column.setRenderer(change);

		columns.add(column);
	

		cm = new ColumnModel(columns);
		
		grid = new Grid<BeanModel>(Contratos.store, cm);
		grid.setAutoExpandColumn("cargo");
		grid.setLoadMask(true);
		grid.setBorders(true);
		grid.addPlugin(sm);
		grid.addPlugin(filters);
		grid.setSelectionModel(sm);
	
	    
	   
        grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {

					@Override
					public void handleEvent(SelectionChangedEvent<BeanModel> be) {
						selectedSize = be.getSelection().size();

						if (selectedSize == 0) {
							panelnuevocontrato.unbind();
							btnDelete.setEnabled(false);
							btnCancelar.setEnabled(false);
							Aportes.empleado_id=null;
						
						} else if (selectedSize == 1) {
						
							selectedModel = be.getSelectedItem();
							if(Contratos.empleado_id!=0){
								panelnuevocontrato.bind(selectedModel);
								btnDelete.setEnabled(true);
								
								Aportes.empleado_id=be.getSelectedItem().get("empleado_id");
								Contratos.contrato_id=	be.getSelectedItem().get("id");
								Integer estadocontrato_id=be.getSelectedItem().get("estadocontrato_id");
								if(estadocontrato_id!=3)
									btnCancelar.setEnabled(true);
							}
							
						} else if (selectedSize > 1) {
							panelnuevocontrato.unbind();
							selectedModels = be.getSelection();
							btnDelete.setEnabled(true);
							btnCancelar.setEnabled(false);
						}

					}

				});

		listener = new Listener<MessageBoxEvent>() {

			@Override
			public void handleEvent(MessageBoxEvent be) {
				String button_id = be.getButtonClicked().getItemId();
				if (Dialog.YES == button_id) {
					if (selectedSize == 1) {
						Integer id = selectedModel.get("id");
						Contratos.delete(id);
					} else if (selectedSize > 1) {
						String ids = Utilities.toIds(selectedModels);
						Contratos.deleteAll(ids);
					}
				}
			}
		};
		sl_delete = new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				String mensaje = "";
				if (selectedSize == 1) {
					String nombres = selectedModel.get("numero");
					mensaje = msg.empleadoEstaSeguro(nombres);

				} else if (selectedSize > 1) {
					mensaje = k.eliminarAll();
				}
				MessageBox.confirm(k.confirmacion(), mensaje, listener)
						.getDialog().getButtonById(Dialog.YES).setText(k.si());

			}
		};

		btnDelete.addSelectionListener(sl_delete);

		setTopComponent(toolbar);
	//	setBottomComponent(toolBarPag);

		add(grid);
	

	}

	
	
}
